/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.adaptor.method.createpropertydefmethod;

//import Inspur.Gsp.Caf.Generator.BaseInfoItem.*;
//import Inspur.Gsp.Caf.Generator.ClassMethodGenerator.*;
//import Inspur.Gsp.Caf.Generator.Utils.*;
//import Inspur.Gsp.Cef.Api.Repository.*;
//import Inspur.Gsp.Cef.Entity.*;
//import Inspur.Gsp.Cef.Entity.Entity.*;
//import Microsoft.CodeAnalysis.CSharp.*;
//import Microsoft.CodeAnalysis.CSharp.Syntax.*;

import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import org.eclipse.jdt.core.dom.*;

//    
//     object obj = reader.ReadValue("123");
//     if(obj==DBNull.Value)
//     return null;
//     return Convert.ToString(obj);
//    
public abstract class NormalPropertyCreateDefMethodGenerator extends BasePropertyCreateDefMethodGenerator {
	protected String propertyName;
	protected IGspCommonField gspCommonField;
	protected static final String varName = "obj";
	protected static final String varReader = "reader";

	protected NormalPropertyCreateDefMethodGenerator(String propertyName, IGspCommonField gspCommonField) {
		this.propertyName = propertyName;
		this.gspCommonField = gspCommonField;
	}


	@Override
	protected  Block buildMethodBody() {

		java.util.ArrayList<Statement> statements = new java.util.ArrayList<Statement>();
//		boolean isString = propertyInfo.getClass().getTypeName() .equals(String.class.getTypeName());
//		statements.add(AdapterGenUtils.getReadValueStatement(ast, varName, propertyName, varReader, isString));
//		addIfStatement(statements);
		addReturnResultStatement(statements);
		Block block = ast.newBlock();
		block.statements().addAll(statements);
		return block;
	}

	protected void addReturnResultStatement(java.util.ArrayList<Statement> statements) {
		ReturnStatement returnStatement = ast.newReturnStatement();
		MethodInvocation getDbProcessorMethodInvocation = ast.newMethodInvocation();
		getDbProcessorMethodInvocation.setName(ast.newSimpleName("getDbProcessor"));
		MethodInvocation methodInvocation = ast.newMethodInvocation(); //RepositoryGenUtil.createMethodInvocation(ast, ast.newSimpleName("dbProcessor"), "toString", null);
		methodInvocation.setExpression(getDbProcessorMethodInvocation);
		methodInvocation.setName(ast.newSimpleName(getDbTypeName()));
		methodInvocation.arguments().add(ast.newSimpleName("reader"));
		StringLiteral stringLiteral = ast.newStringLiteral();
		stringLiteral.setLiteralValue(propertyName);
		methodInvocation.arguments().add(stringLiteral);
		returnStatement.setExpression(methodInvocation);
		statements.add(returnStatement);
	}

	protected String getDbTypeName(){
		return "getStringValue";
	}

	private void addIfStatement(java.util.ArrayList<Statement> statements) {
		IfStatement ifStatement = ast.newIfStatement();
		InfixExpression expression = ast.newInfixExpression();
		expression.setLeftOperand(ast.newSimpleName(varName));
		expression.setRightOperand(ast.newNullLiteral());
		expression.setOperator(InfixExpression.Operator.EQUALS);
		ifStatement.setExpression(expression);

		ifStatement.setThenStatement(getReturnDefaultStatement());

		statements.add(ifStatement);
	}

//	private ReturnStatement getReturnDefaultStatement() {
//		MethodInvocation methodInvocation = RepositoryGenUtil.createMethodInvocation(ast, ast.newName(EntityDataPropertyValueUtils.class.getTypeName()), getDefaultValueMethodName(), null);
//		ReturnStatement returnStatement = ast.newReturnStatement();
//		returnStatement.setExpression(methodInvocation);
//		return returnStatement;
//	}
	protected abstract ReturnStatement getReturnDefaultStatement();
	protected abstract String getDefaultValueMethodName();

	@Override
	protected String getMethodName() {
		return getMethodName(propertyName);
	}

	public static String getMethodName(String propertyName) {
		return String.format("get%1$sNormalPropertyValue", propertyName);
	}
}
